package com.qinglei.recoup.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.utils.RecoupUtil;
import com.qinglei.recoup.system.domain.BedBO;
import com.qinglei.recoup.system.domain.User;
import com.qinglei.recoup.system.enums.RoleEnum;
import com.qinglei.recoup.system.pojo.AllEventParam;
import com.qinglei.recoup.system.pojo.BatchProcEventParam;
import com.qinglei.recoup.system.pojo.BedEventParam;
import com.qinglei.recoup.system.pojo.BedWithEventListVO;
import com.qinglei.recoup.system.pojo.EventItemVO;
import com.qinglei.recoup.system.pojo.EventLogVO;
import com.qinglei.recoup.system.pojo.EventProcParam;
import com.qinglei.recoup.system.pojo.EventProcReportParam;
import com.qinglei.recoup.system.pojo.KeyValueVO;
import com.qinglei.recoup.system.pojo.PageParam;
import com.qinglei.recoup.system.pojo.SearchEventParam;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.EventLogService;
import com.qinglei.recoup.system.service.EventProcService;
import com.qinglei.recoup.system.service.EventService;
import com.qinglei.recoup.system.service.impl.EventLogServiceV1;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "事件相关控制器-已完成")
@Slf4j
@Validated
@RestController
@RequestMapping("event")
public class EventController {

    @Autowired
    private EventService eventService;

    @Resource
    private EventLogService eventLogService;

    @Autowired
    private EventProcService eventProcService;

    @Autowired
    private BedService bedService;

    @ApiOperation(value = "[2.0修改]护工首页页面 处理报警事件接口（消除报警音）")
    @PostMapping("/eventProc")
    public CommonRes<Object> procAlarmEvent(@RequestBody @Valid EventProcParam param) {
        boolean res = eventProcService.procAlarmEvent(param);
        if (!res) {
            return CommonRes.failed("操作失败");
        }
        return CommonRes.ok();
    }

    @Deprecated
    @ApiOperation(value = "护工首页页面 提交处置记录 2.0以后废弃")
    @PostMapping("/procReport")
    public CommonRes submitReport(@RequestBody @Valid EventProcReportParam param) {
        log.info("submitReport param:{}", param);
        CommonRes commonRes;
        try {
            commonRes = eventService.submitReport(param);
        } catch (Exception e) {
            log.error("submitReport error, param:{}", param, e);
            commonRes = CommonRes.failed("操作失败");
        }

        log.info("submitReport result:{}", commonRes);
        return commonRes;
    }

    @Deprecated
    @ApiOperation(value = "[2.0以后废弃]护工首页页面 获取现场状况选项列表")
    @GetMapping("/siteOption")
    public CommonRes<List<KeyValueVO>> getSiteOptionList() {
        List<KeyValueVO> siteOptionList = eventService.getSiteOptionList();
        return CommonRes.ok(siteOptionList);
    }

    @ApiOperation(value = "实时监控页面 获取指定床位事件信息列表(最新和更多) -已完成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bedId", value = "床位ID", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "lastId", value = "上一条事件信息ID，不传或者传0获取最新事件信息", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "获取事件信息数量，默认10条", paramType = "query", dataType = "Integer")
    })
    @GetMapping("/bed")
    public CommonRes<List<EventLogVO>> getBedEventList(@Valid BedEventParam param) {
        log.info("getBedEvent in param:{}", param);
        List<EventLogVO> bedEventList = eventLogService.getBedEventList(param);
        return CommonRes.ok(bedEventList);
    }

    @Deprecated
    @ApiOperation(value = "[废弃]院长首页 获取全院事件信息列表（最新)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lastId", value = "上一条事件信息ID，不传或者传0获取最新事件信息", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "获取事件信息数量，默认10条", paramType = "query", dataType = "Integer")
    })
    @GetMapping("/all")
    public CommonRes<List<EventLogVO>> getAllEvent(@Valid AllEventParam param) {
        log.info("getAllEvent in param:{}", param);
        return eventLogService.getAllEvent(param);
    }

    @Resource
    private EventLogServiceV1 eventLogServiceV1;

    @ApiOperation(value = "院长首页 查询事件信息列表（查看更多）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bedIds", value = "床位ID，多个ID逗号分隔，为空查询全部，为0不查询", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "typeIds", value = "事件类型ID，多个ID逗号分隔，为空查询全部，为0不查询", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "开始日期 格式 2020-11-01 开始和结束日期范围最大31天", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "endDate", value = "结束日期 格式 2020-11-18", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页码，从1开始，默认是1", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数，默认20", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "onlyBed", value = "是否仅查询床位相关事件 1-是 0-否", paramType = "query", dataType = "Integer")
    })
    @GetMapping("/search")
    public CommonRes<IPage<EventLogVO>> searchEvent(@Valid SearchEventParam searchParam, PageParam pageParam) {
        IPage<EventLogVO> eventLogVOIPage = null;

        Integer onlyBed = searchParam.getOnlyBed();
        if(onlyBed==null){
            eventLogVOIPage = eventLogServiceV1.searchEvent(searchParam, pageParam);
        }else if(1==onlyBed){
             eventLogVOIPage = eventLogService.searchEvent(searchParam, pageParam);
        }
        //IPage<EventLogVO> eventLogVOIPage = eventLogService.searchEvent(searchParam, pageParam);

        return CommonRes.ok(eventLogVOIPage);
    }

    @ApiOperation(value = "院长首页 获取事件筛选选项列表-已完成")
    @ApiImplicitParam(name = "onlyBed", value = "是否仅查询床位相关事件筛选项 1-是 0-否", paramType = "query", dataType = "Integer")
    @GetMapping("/eventFilterOption")
    public CommonRes<List<KeyValueVO>> getEventFilterOption(Integer onlyBed) {
        List<KeyValueVO> eventFilterOption = eventService.getEventFilterOption(onlyBed);
        return CommonRes.ok(eventFilterOption);
    }
//    @ApiOperation(value = "【2.0新增】获取已接单事件列表")
//    @ApiImplicitParam(name = "bedId", value = "床位ID,传ID，查询指定床位的已接单事件列表，不传ID查询该用户设置为自动接单的一个床位的已接单列表", paramType = "query", dataType = "Integer")
//    @GetMapping("/takenList")
//    public CommonRes<BedWithEventListVO> getTakenList(Integer bedId) {
//        EventItemVO eventItemVO = new EventItemVO();
//        eventItemVO.setEventId(2L);
//        eventItemVO.setDate("12-20");
//        eventItemVO.setTime("19:03");
//        eventItemVO.setEventName("离床过久");
//        EventItemVO eventItemVO1 = new EventItemVO();
//        eventItemVO1.setEventId(1L);
//        eventItemVO1.setDate("12-20");
//        eventItemVO1.setTime("18:00~18:30");
//        eventItemVO1.setEventName("离床过久");
//        List<EventItemVO> eventList = new ArrayList<>();
//        eventList.add(eventItemVO);
//        eventList.add(eventItemVO1);
//        BedWithEventListVO bedWithEventListVO = new BedWithEventListVO();
//        bedWithEventListVO.setBedId(bedId == null ? 1 : bedId);
//        bedWithEventListVO.setBedName("假数据床位");
//        bedWithEventListVO.setEventList(eventList);
//        return CommonRes.ok(bedWithEventListVO);
//    }

    @ApiOperation(value = "【2.0新增】获取遗漏事件列表-已完成")
    @ApiImplicitParam(name = "bedId", value = "床位ID", paramType = "query", dataType = "Integer", required = true)
    @GetMapping("/missedList")
    public CommonRes<BedWithEventListVO> getMissedList(Integer bedId) {
        BedBO bedBO = bedService.getBedFullName(bedId);
        if (bedBO == null) {
            return CommonRes.failed("床位不存在");
        }
        User user = RecoupUtil.getCurrentUser();
        BedWithEventListVO bedWithEventListVO = new BedWithEventListVO();
        bedWithEventListVO.setBedId(bedId);
        if (user.getRoleId().equals(String.valueOf(RoleEnum.NURSE.getCode()))) {
            bedWithEventListVO.setBedName(bedBO.getFloorName() + "-" + bedBO.getRoomName() + "-" + bedBO.getBedName());
        } else {
            bedWithEventListVO.setBedName(bedBO.getBuildingName() + "-" + bedBO.getFloorName() + "-" + bedBO.getRoomName() + "-" + bedBO.getBedName());
        }
        List<EventItemVO> missedEventListByBed = eventProcService.getMissedEventListByBed(bedId);
        bedWithEventListVO.setEventList(missedEventListByBed);
//        EventItemVO eventItemVO = new EventItemVO();
//        eventItemVO.setEventId(2L);
//        eventItemVO.setDate("12-20");
//        eventItemVO.setTime("19:03~19:15");
//        eventItemVO.setEventName("离床过久");
//        EventItemVO eventItemVO1 = new EventItemVO();
//        eventItemVO1.setEventId(1L);
//        eventItemVO1.setDate("12-20");
//        eventItemVO1.setTime("18:00~18:30");
//        eventItemVO1.setEventName("离床过久");
//        List<EventItemVO> eventList = new ArrayList<>();
//        eventList.add(eventItemVO);
//        eventList.add(eventItemVO1);
//        BedWithEventListVO bedWithEventListVO = new BedWithEventListVO();
//        bedWithEventListVO.setBedId(bedId == null ? 1 : bedId);
//        bedWithEventListVO.setBedName("假数据床位");
//        bedWithEventListVO.setEventList(eventList);
        return CommonRes.ok(bedWithEventListVO);
    }

    @ApiOperation(value = "【2.0新增】护工首页页面 批量处理遗漏事件-已完成")
    @PostMapping("/submitMissedEvent")
    public CommonRes<Object> submitMissedEvent(@RequestBody @Valid BatchProcEventParam param) {
        eventProcService.procMissedEvent(param);
        return CommonRes.ok();
    }
}
